#include "client.h"
#include "client_global.h"
#include "client_impl.h"

using namespace bhttp;

client::client(const std::string& host, int timeout, const std::string& ca)
{
    // 静态变量，自动调用一次curl_global_init/curl_global_cleanup
    static client_global cglobal;
    // 创建IMPL类
    impl_ = std::make_shared<client_impl>(host, timeout, ca);
    // 启动线程队列
    impl_->thd_->start();
}

client::~client()
{
    // 停止线程队列
    impl_->thd_->stop();
}

void client::timeout(int secs)
{
    impl_->timeout(secs);
}

int client::timeout() const
{
    return impl_->timeout_;
}

void client::capath(const std::string& ca)
{
    impl_->capath(ca);
}

const std::string client::capath() const
{
    return impl_->capath_;
}

void client::host(const std::string& host)
{
    impl_->host(host);
}

const std::string client::host() const
{
    return impl_->host_;
}

void client::send(const request_ptr& req, const response_callback& res_cb)
{
    impl_->thd_->run(std::bind(&client_impl::process, impl_, req, res_cb));
}

